<?php 
class Admin_CategoryController extends Zend_Controller_Action{
	public function init(){
        $layoutPath = APP_PATH . '\templates\admin\\';
        
        $option = array ('layout'     => 'index',
                         'layoutPath' => $layoutPath,
                         'contentKey' => 'content' );
        Zend_Layout::startMvc( $option );
    }
	
	public function indexAction(){
		$cat = new Admin_Model_Category();
		$arrcat = $cat->getAllCategory();             
		$this->view->data = $arrcat;	
	}
	
	public function addAction(){
		$cat = new Admin_Model_Category();
		//$cat->addCategory('bai1','asd','hsfsdg');
		if($this->_request->isPost()){
			$catname = $this->_request->getParam('txtCatName');
            $catdes  = $this->_request->getParam('txtDes');
            //$catimg  = $this->_request->getParam('txtFile');
			//upload image
			$file     = $_FILES['txtFile']; 
			$url      = APP_PATH.'/templates/admin/images/upload/category/';
			$arrAllowEx=array('jpg','jpeg','png','gif','bmp');
			$maxSize  =5242880;
			$catimg    =$cat->uploadFile($file,$url,$arrAllowEx,$maxSize);
			$err = '';
			$i = 1;
			$data=array();
			if($catname==''){
				$err.=$i.' Catname must be not null.</br>';
                $i++;
			}else{
				$data['cat_name']=$catname;
			}
			if($catdes==''){
				$err.=$i.' Catname must be not null.</br>';
                $i++;
			}else{
				$data['cat_des']=$catdes;
			}
			if($catimg==''){
				$err.=$i.' Catname must be not null.</br>';
                $i++;
			}else{
				$data['cat_des']=$catimg;
			}
			
			if($err!=''){
                $this->view->err = $err;
                $this->view->cat = $data;
             }else{
                $check = $cat->addCategory($catname,$catdes,$catimg);
                if($check){
                    $this->_redirect('/admin/category/index');
                }
             }
			
		}
	}
	
	public function editAction(){
		$cat = new Admin_Model_Category();
		echo $id  = $this->_request->getParam('id');
        echo $catname = $this->_request->getParam('txtCatName');    
		//echo $catname;die;
        echo $catdes = $this->_request->getParam('txtCatDes');       
        
      
		if($this->_request->isPost()){ 
			$file     = $_FILES['txtFile'];  
			$url      = APP_PATH.'/templates/admin/images/upload/category/';
			$arrAllowEx=array('jpg','jpeg','png','gif','bmp');
			$maxSize  =5242880;
			$catimg    =$cat->uploadFile($file,$url,$arrAllowEx,$maxSize);
            $err='';
            $i=1;
             
             if($catname==''){
                $err.=$i.' Catname be not null.</br>';
                $data['cat_name']='';
                $i++;
             }else{
                $data['cat_name']=$catname;
             }
			 if($catdes==''){
                $err.=$i.' Catdes be not null.</br>';
                $data['cat_des']='';
                $i++;
             }else{
                $data['cat_des']=$catdes;
             }  
			 if($catimg==''){
                $err.=$i.' Catdes be not null.</br>';
                $data['cat_img']='';
                $i++;
             }else{
                $data['cat_des']=$catimg;
             }
             if($err!=''){
                $this->view->err = $err;
                $this->view->cat = $data;
             }else{
                $check = $cat->editCategory($catname,$catdes,$catimg,$id);
                if($check){
                    $this->_redirect('/admin/category/index');
                }
             }
             
		}else{
		      $this->view->cat = $cat->getCategory($id);
		}
	}
	
	public function deleteAction(){
		$cat = new Admin_Model_Category();
        $id = $this->_request->getParam('id');
		$check = $cat ->delete('cat_id IN('.$id.')');
		if($check){
			$this->_redirect('/admin/category/index');
		}
	}
}